//对于axios函数库进行二次封装

import axios from 'axios'
import { ElMessage } from 'element-plus';
import useUserStore from '@/store/modules/user';
//利用axios.create创建axios实例
const request = axios.create({
    baseURL: '/',
    timeout: 5000
});

request.interceptors.request.use((config) => {
    //config：请求拦截器回调注入的对象，
    let userStore = useUserStore()
    //用户登录需要携带
    if(userStore.userToken){
        
        
        config.headers.Authorization = userStore.userToken
    }
    return config;
})

request.interceptors.response.use((response) => {
    return response.data;
}, (error) => {
    let status = error.response.status;
    switch (status) {
        case 404:
            ElMessage({
                type: 'error',
                message: '请求路径出现错误'
            })
            break;
        case 500 | 501 | 502 | 503 | 504 | 505:
            ElMessage({
                type: 'error',
                message: '呜呜呜，没有钱续服务器了'
            })
        case 402:
            ElMessage({
                type: 'error',  
                message: '参数有误'
            })
            break;
    }
    return Promise.reject(new Error(error.message));
})

export default request;